package org.destiny.hello.world.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 请求login和login_view不会被拦截，其他页面会被拦截重定向到login_view页面，只有在登录之后才不会被拦截
 * 
 * @author destinys
 *
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/index")
    public String index() {
        return "index";
    }

    /**
     * 如果是get请求的话，那么一定要带user，否则，不会请求进来，如果是非必传的话，需要注解声明
     * 
     * @param request
     * @param user
     * @return
     */
    @RequestMapping("/login")
    public String login(HttpServletRequest request, @RequestParam("user") String user) {
        request.getSession().setAttribute("_session_user", user);
        return "login";
    }

    @RequestMapping("/exit")
    public String exit(HttpServletRequest request, @RequestParam("user") String user) {
        request.getSession().removeAttribute("_session_user");
        return "login";
    }

    @RequestMapping("/login_view")
    public String loginView() {
        return "login_view";
    }
}
